package jogo;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.Window;

import javax.swing.ImageIcon;

/**
 * Created on Nov 16, 2002 - 5:21:37 AM
 * 
 * @author GUJ
 */
public class Splash extends Window {
  /**
     * 
     */
  private static final long serialVersionUID = 1L;

  public static String message = "Jogo Swing v1.0";

  private final String imgName = "splash.png";
  Image splashImage;
  Toolkit toolkit;
  private static Splash splash;

  static {
	splash = new Splash();
  }

  private Splash() {
	super(new Frame());
	setVisible(false);
	splashImage = null;
	toolkit = Toolkit.getDefaultToolkit();
  }

  public Image getImagemSplash() {

	if (splashImage == null) {
	  splashImage = (new ImageIcon(Splash.class.getResource("/resources/splash.png"))).getImage();
	}

	return splashImage;
  }

  public static Splash getInstance() {
	return splash;
  }

  /**
   * Method initSplash.
   */
  private void initSplash() {
	MediaTracker media = new MediaTracker(this);

	if (getImagemSplash() != null) {
	  media.addImage(getImagemSplash(), 0);

	  try {
		media.waitForID(0);
	  } catch (InterruptedException ie) {
	  }
	}

	// Configura o tamanho do splash e a posicao na tela
	setSize(getImagemSplash().getWidth(this), getImagemSplash().getHeight(this));

	Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	Dimension size = getSize();

	if (size.width > screenSize.width) {
	  size.width = screenSize.width;

	}
	if (size.height > screenSize.height) {
	  size.height = screenSize.height;

	}
	setLocation((screenSize.width - size.width) / 2, (screenSize.height - size.height) / 2);
	setVisible(true);
  }

  public void openSplash() {
	setCursor(new Cursor(Cursor.WAIT_CURSOR));
	initSplash();
  }

  public void finish() {
	setVisible(false);
	dispose();
  }

  public void paint(Graphics g) {
	this.setForeground(Color.WHITE);
	g.drawImage(getImagemSplash(), 0, 0, getBackground(), this);
	g.setFont(new Font("Arial", Font.ITALIC, 11));
	g.drawString(message, 5, 143);
  }

}
